#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

void settime(int signum)
{
    cout<<"signum is :"<<signum<<" pid is: "<<getpid()<<endl;
	alarm(2);
}

int main()
{
	signal(SIGALRM,settime);
	alarm(2);
	while(1)
	{
        sleep(1);
        cout<<"my pid is :"<<getpid()<<endl;
	}
	return 0;
}










/*
int main()
{
      pid_t id = fork();
      if(id == 0)
      {
          int cnt = 500;
          while(cnt)
          {
              cout << "i am a child process, pid: " << getpid() << " cnt: " << cnt << endl;
              sleep(1);
              cnt--;
          }

          exit(0);
      }

      int status = 0;
      pid_t rid = waitpid(id, &status, 0);
      if(rid == id)
      {
          cout << "child quit info, rid: " << rid << " exit code: " << ((status>>8)&0xFF) 
          << " exit signal: " << (status&0x7F) 
          <<" core dump: " << ((status>>7)&1) << endl;  
      }
}
*/






/* int main() {
    int *ptr = (int *)malloc(sizeof(int));   分配内存
    *ptr = 42;   使用指针

    free(ptr);   释放内存
    printf("Accessing freed memory: %d\n", *ptr);   访问已释放的内存（野指针错误）

    return 0;
} */